package neusoft.edu.cn.a215lmeordering.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.squareup.picasso.Picasso;

import java.util.List;

import neusoft.edu.cn.a215lmeordering.R;
import neusoft.edu.cn.a215lmeordering.ViewHolder.ShopFoodItemViewHolder;
import neusoft.edu.cn.a215lmeordering.ViewHolder.ShopItemViewHolder;
import neusoft.edu.cn.a215lmeordering.bean.Food;
import neusoft.edu.cn.a215lmeordering.bean.Shop;
import neusoft.edu.cn.a215lmeordering.iface.OnItemClickListener;

public class ShopFoodAdapter extends RecyclerView.Adapter<ShopFoodItemViewHolder> {
    private List<Food> shopDateList;
    private OnItemClickListener myOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener){
        myOnItemClickListener=listener;
    }

    public void setDate(List<Food> dateItemList){
        this.shopDateList=dateItemList;
        notifyDataSetChanged();//通知数据集发生变化
    }

    @NonNull
    @Override
    public ShopFoodItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
        View itemView = layoutInflater.inflate(R.layout.itemfood_layout,viewGroup,false);
        ShopFoodItemViewHolder viewHolder = new ShopFoodItemViewHolder(itemView);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(@NonNull final ShopFoodItemViewHolder itemViewHolder, final int i) {
        final Food shopList = shopDateList.get(i);

        itemViewHolder.textView1.setText(shopList.getFoodname());
        itemViewHolder.textView2.setText(shopList.getIntro());
        itemViewHolder.textView3.setText("单价："+shopList.getPrice());

        Picasso.get()
                .load("http://172.24.10.175:8080/foodService"+shopList.getPic())
                .into(itemViewHolder.imageView);

        itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myOnItemClickListener.onItemClick(view,i);
            }
        });

    }

    @Override
    public int getItemCount() {
        if(shopDateList!=null)
            return shopDateList.size();
        else
            return 0;
//        return shopDateList.size();//返回列表显示的数据个数
    }
}
